/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ibrae.mchs.radiation.entities.controllers;

import ru.ibrae.mchs.radiation.entities.RajonOld;
import ru.ibrae.mchs.radiation.entities.controllers.exceptions.IllegalOrphanException;
import ru.ibrae.mchs.radiation.entities.controllers.exceptions.NonexistentEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ru.ibrae.mchs.radiation.entities.SubjectRf;
import ru.ibrae.mchs.radiation.entities.Result;
import java.util.HashSet;
import java.util.Set;
import ru.ibrae.mchs.radiation.entities.Selsovet;
import ru.ibrae.mchs.radiation.entities.Geoobject;
import java.util.ArrayList;

/**
 *
 * @author murr
 */
public class RajonOldJpaController {

    public RajonOldJpaController() {
        emf = Persistence.createEntityManagerFactory("DesktopApplication4PU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(RajonOld rajonOld) {
        if (rajonOld.getResultSet() == null) {
            rajonOld.setResultSet(new HashSet<Result>());
        }
        if (rajonOld.getSelsovetSet() == null) {
            rajonOld.setSelsovetSet(new HashSet<Selsovet>());
        }
        if (rajonOld.getGeoobjectSet() == null) {
            rajonOld.setGeoobjectSet(new HashSet<Geoobject>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            SubjectRf subjectRf = rajonOld.getSubjectRf();
            if (subjectRf != null) {
                subjectRf = em.getReference(subjectRf.getClass(), subjectRf.getId());
                rajonOld.setSubjectRf(subjectRf);
            }
            Set<Result> attachedResultSet = new HashSet<Result>();
            for (Result resultSetResultToAttach : rajonOld.getResultSet()) {
                resultSetResultToAttach = em.getReference(resultSetResultToAttach.getClass(), resultSetResultToAttach.getIdRezult());
                attachedResultSet.add(resultSetResultToAttach);
            }
            rajonOld.setResultSet(attachedResultSet);
            Set<Selsovet> attachedSelsovetSet = new HashSet<Selsovet>();
            for (Selsovet selsovetSetSelsovetToAttach : rajonOld.getSelsovetSet()) {
                selsovetSetSelsovetToAttach = em.getReference(selsovetSetSelsovetToAttach.getClass(), selsovetSetSelsovetToAttach.getIdSelsov());
                attachedSelsovetSet.add(selsovetSetSelsovetToAttach);
            }
            rajonOld.setSelsovetSet(attachedSelsovetSet);
            Set<Geoobject> attachedGeoobjectSet = new HashSet<Geoobject>();
            for (Geoobject geoobjectSetGeoobjectToAttach : rajonOld.getGeoobjectSet()) {
                geoobjectSetGeoobjectToAttach = em.getReference(geoobjectSetGeoobjectToAttach.getClass(), geoobjectSetGeoobjectToAttach.getIdNp());
                attachedGeoobjectSet.add(geoobjectSetGeoobjectToAttach);
            }
            rajonOld.setGeoobjectSet(attachedGeoobjectSet);
            em.persist(rajonOld);
            if (subjectRf != null) {
                subjectRf.getRajonOldSet().add(rajonOld);
                subjectRf = em.merge(subjectRf);
            }
            for (Result resultSetResult : rajonOld.getResultSet()) {
                RajonOld oldRajonOldOfResultSetResult = resultSetResult.getRajonOld();
                resultSetResult.setRajonOld(rajonOld);
                resultSetResult = em.merge(resultSetResult);
                if (oldRajonOldOfResultSetResult != null) {
                    oldRajonOldOfResultSetResult.getResultSet().remove(resultSetResult);
                    oldRajonOldOfResultSetResult = em.merge(oldRajonOldOfResultSetResult);
                }
            }
            for (Selsovet selsovetSetSelsovet : rajonOld.getSelsovetSet()) {
                RajonOld oldRajonOldOfSelsovetSetSelsovet = selsovetSetSelsovet.getRajonOld();
                selsovetSetSelsovet.setRajonOld(rajonOld);
                selsovetSetSelsovet = em.merge(selsovetSetSelsovet);
                if (oldRajonOldOfSelsovetSetSelsovet != null) {
                    oldRajonOldOfSelsovetSetSelsovet.getSelsovetSet().remove(selsovetSetSelsovet);
                    oldRajonOldOfSelsovetSetSelsovet = em.merge(oldRajonOldOfSelsovetSetSelsovet);
                }
            }
            for (Geoobject geoobjectSetGeoobject : rajonOld.getGeoobjectSet()) {
                RajonOld oldRajonOldOfGeoobjectSetGeoobject = geoobjectSetGeoobject.getRajonOld();
                geoobjectSetGeoobject.setRajonOld(rajonOld);
                geoobjectSetGeoobject = em.merge(geoobjectSetGeoobject);
                if (oldRajonOldOfGeoobjectSetGeoobject != null) {
                    oldRajonOldOfGeoobjectSetGeoobject.getGeoobjectSet().remove(geoobjectSetGeoobject);
                    oldRajonOldOfGeoobjectSetGeoobject = em.merge(oldRajonOldOfGeoobjectSetGeoobject);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(RajonOld rajonOld) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            RajonOld persistentRajonOld = em.find(RajonOld.class, rajonOld.getIdRajonO());
            SubjectRf subjectRfOld = persistentRajonOld.getSubjectRf();
            SubjectRf subjectRfNew = rajonOld.getSubjectRf();
            Set<Result> resultSetOld = persistentRajonOld.getResultSet();
            Set<Result> resultSetNew = rajonOld.getResultSet();
            Set<Selsovet> selsovetSetOld = persistentRajonOld.getSelsovetSet();
            Set<Selsovet> selsovetSetNew = rajonOld.getSelsovetSet();
            Set<Geoobject> geoobjectSetOld = persistentRajonOld.getGeoobjectSet();
            Set<Geoobject> geoobjectSetNew = rajonOld.getGeoobjectSet();
            List<String> illegalOrphanMessages = null;
            for (Selsovet selsovetSetOldSelsovet : selsovetSetOld) {
                if (!selsovetSetNew.contains(selsovetSetOldSelsovet)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Selsovet " + selsovetSetOldSelsovet + " since its rajonOld field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (subjectRfNew != null) {
                subjectRfNew = em.getReference(subjectRfNew.getClass(), subjectRfNew.getId());
                rajonOld.setSubjectRf(subjectRfNew);
            }
            Set<Result> attachedResultSetNew = new HashSet<Result>();
            for (Result resultSetNewResultToAttach : resultSetNew) {
                resultSetNewResultToAttach = em.getReference(resultSetNewResultToAttach.getClass(), resultSetNewResultToAttach.getIdRezult());
                attachedResultSetNew.add(resultSetNewResultToAttach);
            }
            resultSetNew = attachedResultSetNew;
            rajonOld.setResultSet(resultSetNew);
            Set<Selsovet> attachedSelsovetSetNew = new HashSet<Selsovet>();
            for (Selsovet selsovetSetNewSelsovetToAttach : selsovetSetNew) {
                selsovetSetNewSelsovetToAttach = em.getReference(selsovetSetNewSelsovetToAttach.getClass(), selsovetSetNewSelsovetToAttach.getIdSelsov());
                attachedSelsovetSetNew.add(selsovetSetNewSelsovetToAttach);
            }
            selsovetSetNew = attachedSelsovetSetNew;
            rajonOld.setSelsovetSet(selsovetSetNew);
            Set<Geoobject> attachedGeoobjectSetNew = new HashSet<Geoobject>();
            for (Geoobject geoobjectSetNewGeoobjectToAttach : geoobjectSetNew) {
                geoobjectSetNewGeoobjectToAttach = em.getReference(geoobjectSetNewGeoobjectToAttach.getClass(), geoobjectSetNewGeoobjectToAttach.getIdNp());
                attachedGeoobjectSetNew.add(geoobjectSetNewGeoobjectToAttach);
            }
            geoobjectSetNew = attachedGeoobjectSetNew;
            rajonOld.setGeoobjectSet(geoobjectSetNew);
            rajonOld = em.merge(rajonOld);
            if (subjectRfOld != null && !subjectRfOld.equals(subjectRfNew)) {
                subjectRfOld.getRajonOldSet().remove(rajonOld);
                subjectRfOld = em.merge(subjectRfOld);
            }
            if (subjectRfNew != null && !subjectRfNew.equals(subjectRfOld)) {
                subjectRfNew.getRajonOldSet().add(rajonOld);
                subjectRfNew = em.merge(subjectRfNew);
            }
            for (Result resultSetOldResult : resultSetOld) {
                if (!resultSetNew.contains(resultSetOldResult)) {
                    resultSetOldResult.setRajonOld(null);
                    resultSetOldResult = em.merge(resultSetOldResult);
                }
            }
            for (Result resultSetNewResult : resultSetNew) {
                if (!resultSetOld.contains(resultSetNewResult)) {
                    RajonOld oldRajonOldOfResultSetNewResult = resultSetNewResult.getRajonOld();
                    resultSetNewResult.setRajonOld(rajonOld);
                    resultSetNewResult = em.merge(resultSetNewResult);
                    if (oldRajonOldOfResultSetNewResult != null && !oldRajonOldOfResultSetNewResult.equals(rajonOld)) {
                        oldRajonOldOfResultSetNewResult.getResultSet().remove(resultSetNewResult);
                        oldRajonOldOfResultSetNewResult = em.merge(oldRajonOldOfResultSetNewResult);
                    }
                }
            }
            for (Selsovet selsovetSetNewSelsovet : selsovetSetNew) {
                if (!selsovetSetOld.contains(selsovetSetNewSelsovet)) {
                    RajonOld oldRajonOldOfSelsovetSetNewSelsovet = selsovetSetNewSelsovet.getRajonOld();
                    selsovetSetNewSelsovet.setRajonOld(rajonOld);
                    selsovetSetNewSelsovet = em.merge(selsovetSetNewSelsovet);
                    if (oldRajonOldOfSelsovetSetNewSelsovet != null && !oldRajonOldOfSelsovetSetNewSelsovet.equals(rajonOld)) {
                        oldRajonOldOfSelsovetSetNewSelsovet.getSelsovetSet().remove(selsovetSetNewSelsovet);
                        oldRajonOldOfSelsovetSetNewSelsovet = em.merge(oldRajonOldOfSelsovetSetNewSelsovet);
                    }
                }
            }
            for (Geoobject geoobjectSetOldGeoobject : geoobjectSetOld) {
                if (!geoobjectSetNew.contains(geoobjectSetOldGeoobject)) {
                    geoobjectSetOldGeoobject.setRajonOld(null);
                    geoobjectSetOldGeoobject = em.merge(geoobjectSetOldGeoobject);
                }
            }
            for (Geoobject geoobjectSetNewGeoobject : geoobjectSetNew) {
                if (!geoobjectSetOld.contains(geoobjectSetNewGeoobject)) {
                    RajonOld oldRajonOldOfGeoobjectSetNewGeoobject = geoobjectSetNewGeoobject.getRajonOld();
                    geoobjectSetNewGeoobject.setRajonOld(rajonOld);
                    geoobjectSetNewGeoobject = em.merge(geoobjectSetNewGeoobject);
                    if (oldRajonOldOfGeoobjectSetNewGeoobject != null && !oldRajonOldOfGeoobjectSetNewGeoobject.equals(rajonOld)) {
                        oldRajonOldOfGeoobjectSetNewGeoobject.getGeoobjectSet().remove(geoobjectSetNewGeoobject);
                        oldRajonOldOfGeoobjectSetNewGeoobject = em.merge(oldRajonOldOfGeoobjectSetNewGeoobject);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = rajonOld.getIdRajonO();
                if (findRajonOld(id) == null) {
                    throw new NonexistentEntityException("The rajonOld with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            RajonOld rajonOld;
            try {
                rajonOld = em.getReference(RajonOld.class, id);
                rajonOld.getIdRajonO();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The rajonOld with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Set<Selsovet> selsovetSetOrphanCheck = rajonOld.getSelsovetSet();
            for (Selsovet selsovetSetOrphanCheckSelsovet : selsovetSetOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This RajonOld (" + rajonOld + ") cannot be destroyed since the Selsovet " + selsovetSetOrphanCheckSelsovet + " in its selsovetSet field has a non-nullable rajonOld field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            SubjectRf subjectRf = rajonOld.getSubjectRf();
            if (subjectRf != null) {
                subjectRf.getRajonOldSet().remove(rajonOld);
                subjectRf = em.merge(subjectRf);
            }
            Set<Result> resultSet = rajonOld.getResultSet();
            for (Result resultSetResult : resultSet) {
                resultSetResult.setRajonOld(null);
                resultSetResult = em.merge(resultSetResult);
            }
            Set<Geoobject> geoobjectSet = rajonOld.getGeoobjectSet();
            for (Geoobject geoobjectSetGeoobject : geoobjectSet) {
                geoobjectSetGeoobject.setRajonOld(null);
                geoobjectSetGeoobject = em.merge(geoobjectSetGeoobject);
            }
            em.remove(rajonOld);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<RajonOld> findRajonOldEntities() {
        return findRajonOldEntities(true, -1, -1);
    }

    public List<RajonOld> findRajonOldEntities(int maxResults, int firstResult) {
        return findRajonOldEntities(false, maxResults, firstResult);
    }

    private List<RajonOld> findRajonOldEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(RajonOld.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public RajonOld findRajonOld(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(RajonOld.class, id);
        } finally {
            em.close();
        }
    }

    public int getRajonOldCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<RajonOld> rt = cq.from(RajonOld.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
